extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/z/OptionButton
onready var name_input = $PanelContainer/VBoxContainer/name/LineEdit

onready var next_select = $PanelContainer/VBoxContainer/next/SearchSelect
onready var dir_op = $PanelContainer/VBoxContainer/dir/OptionButton
onready var res_input = $PanelContainer/VBoxContainer/res/LineEdit
onready var res_yoffset_input = $PanelContainer/VBoxContainer/resOffset/LineEdit
onready var anim_type_op = $PanelContainer/VBoxContainer/door_anim_type/OptionButton
onready var width_input = $PanelContainer/VBoxContainer/width/SpinBox

var edit_data = null




func set_edit_data(data):
	edit_data = data
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	var position_info = map_view.get_select_position_info()
	var door_list = DataSource.get_data_list(DataSource.map_door)
	
	dir_op.set_item_id(0, -1)
	
	
	next_select.set_data("选择传送门!", door_list)
		
	if edit_data:
		$PanelContainer/VBoxContainer/position/position.text = str("(",data.x, " ", data.y, ")")
		
		name_input.text = edit_data["name"]
		
		res_input.text = data["res"]
		res_yoffset_input.text = str(data.get("resOffset",0))
		dir_op.select(dir_op.get_item_index(data.get("dir", 1)))
		anim_type_op.select(anim_type_op.get_item_index(data["doorAnimType"]))
		width_input.value = data["width"]
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑传送门： %s" % data["name"]
		
		next_select.set_value(edit_data["nextId"])
	else:
		next_select.set_value(-1)

func state_update():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	# 更新当前坐标
	var position_info = map_view.get_select_position_info()
	$PanelContainer/VBoxContainer/position/position.text = str("(",position_info.x, " ", position_info.y, ")")
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	if edit_data:
		read_form(edit_data)
		
		if !edit_data["name"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		DataSource.update_data(DataSource.map_door, edit_data)
		
		MainTool.toast_info("更新传送门%s成功!" % edit_data["name"])
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"z": 1,
			"mapId": position_info["map_register"]["id"]
		}
		
		read_form(data)
		
		if !data["name"]: 
			MainTool.toast_info("名称不能为空!")
			return
		
		var pre_list = DataSource.get_data_by_list(DataSource.map_door, position_info["map_register"]["id"], "mapId")
		for pre in pre_list:
			if pre["x"] == position_info.x and pre["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_door, data)
		
		MainTool.toast_info("添加传送门成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()


func read_form(dic):
	dic["name"] = name_input.text
	dic["nextId"] = next_select.get_value()
	dic["dir"] = dir_op.get_selected_id()
	dic["res"] = res_input.text
	dic["doorAnimType"] = anim_type_op.get_selected_id()
	dic["width"] = width_input.value
	dic["resOffset"] = int(res_yoffset_input.text)
	

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
